﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.SystemSetting;
using Model.SystemSetting;

namespace ManagementSystem.Controllers.SystemSettings
{
    public class LoginManagingController : Controller
    {
        private LoginManagingBll lmBll = new LoginManagingBll();

        // GET: /LoginManaging/

        public ActionResult Index()
        {
            List<UserInfo> UserLi = lmBll.fetchUsersList();
            return View(UserLi);
        }

        [HttpPost]
        public ActionResult Index(string userName)
        {
            List<UserInfo> UserLi = lmBll.fetchUsersList();
            var result = UserLi.Where(t => t.UserName == userName).ToList();
            return View(result);
        }

        [HttpGet]
        public ActionResult CreateUser()
        {          
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateUser(UserInfo user)
        {
            user.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                if (lmBll.SaveUser(user) > 0)
                {
                    return RedirectToAction("Index");
                }                
            }
            return View(user);
        }

        [HttpGet]
        public ActionResult EditUser(int? id)
        {
            if (id != null)
            {
                UserInfo ui = lmBll.FetchSingleUser(id.Value);
                return View(ui);
            }
            return RedirectToAction("Index");
        }
            
        [HttpPost]
        public ActionResult EditUser(UserInfo user)
        {
            if (ModelState.IsValid)
            {
                if (lmBll.UpdateUser(user) > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(user);
        }

        [HttpGet]
        public ActionResult DeleteUser(int? id)
        {
            if (id != null)
            {
                UserInfo ui = lmBll.FetchSingleUser(id.Value);
                if (lmBll.DeleteSingleUser(ui) > 0)
                {
                    return RedirectToAction("Index");
                }               
            }
            return RedirectToAction("Index");
        }

    }
}
